﻿using System;
using System.Collections.Generic;
using System.Linq;
using Lidgren.Network;

namespace HFServer
{
    class Program
    {
        // Server object
        static NetServer Server;
        // Configuration object
        static NetPeerConfiguration Config;

        enum PacketTypes
        {
            LOGIN,
            MOVE,
            WORLDSTATE
        }

        static void Main(string[] args)
        {

            // Create new instance of configs. Parameter is "application Id". It has to be same on client and server.
            Config = new NetPeerConfiguration("HF");

            // Set server port
            Config.Port = 14242;

            // Max client amount
            Config.MaximumConnections = 8;

            // Enable New messagetype. Explained later
            Config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);

            // Create new server based on the configs just defined
            Server = new NetServer(Config);

            // Start it
            Server.Start();

            

            Console.WriteLine("Server started " + System.Environment.UserName);

            // Object that can be used to store and read messages
            NetIncomingMessage inc;

            while (true)
            {
                // Server.ReadMessage() Returns new messages, that have not yet been read.
                // If "inc" is null -> ReadMessage returned null -> Its null, so dont do this :)
                if ((inc = Server.ReadMessage()) != null)
                {
                    // Theres few different types of messages. To simplify this process, i left only 2 of em here
                    switch (inc.MessageType)
                    {
                        // If incoming message is Request for connection approval
                        // This is the very first packet/message that is sent from client
                        // Here you can do new player initialisation stuff
                        case NetIncomingMessageType.ConnectionApproval:
                            
                            // Read the first byte of the packet
                            // ( Enums can be casted to bytes, so it be used to make bytes human readable )
                            if (inc.ReadByte() == (byte)PacketTypes.LOGIN)
                            {
                                Console.WriteLine("Incoming LOGIN= " + inc.SenderConnection);

                                // Approve clients connection ( Its sort of agreenment. "You can be my client and i will host you" )
                                inc.SenderConnection.Approve();

                            }

                            break;
                          case NetIncomingMessageType.Data:

                            // Read first byte
                            if (inc.ReadByte() == (byte)PacketTypes.MOVE)
                            {


                                // Read next byte
                                byte b = inc.ReadByte();
                                float X = inc.ReadFloat();
                                float Y = inc.ReadFloat();
                                Console.WriteLine("PlayerX = " + X + " PlayerY=" + Y + " PlayerDir=" + b);

                            }

                            
                            break;

                          case NetIncomingMessageType.DiscoveryRequest:

                            // Create a response and write some example data to it
                            NetOutgoingMessage response = Server.CreateMessage();
                            response.Write("Server name=" + Server.Configuration.BroadcastAddress);

                            // Send the response to the sender of the request
                            Server.SendDiscoveryResponse(response, inc.SenderEndpoint);
                            break;
                        
                        default:
                            
                            break;

                      }

                    }

            }

            
        }
    }
}
